import 'package:boss/color_constant.dart';
import 'package:boss/models/coupon.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

class CouponItem extends StatelessWidget {
  final Coupon coupon;
  final VoidCallback onSendCouponTap;
  final VoidCallback onTap;
  final int type;

  const CouponItem(this.coupon, this.type, {this.onSendCouponTap, this.onTap, Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final bgMap = {
      Coupon.TYPE_CASH: 'assets/image/coupon_cash_bg.png',
      Coupon.TYPE_DISCOUNT: 'assets/image/coupon_discount_bg.png',
    };
    final typeMap = {
      Coupon.TYPE_CASH: '代金券',
      Coupon.TYPE_DISCOUNT: '折扣券',
    };
    String timeRange;
    if (coupon.timeLimitType == Coupon.TIME_ABSOLUTELY) {
      timeRange = '${coupon.beginTime}至${coupon.endTime}';
    } else if (coupon.timeLimitType == Coupon.TIME_TODAY) {
      timeRange = '自领券起${coupon.relativeDays}天内使用';
    } else if (coupon.timeLimitType == Coupon.TIME_TOMORROW) {
      timeRange = '自领卷起次日${coupon.relativeDays}天内使用';
    }

    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.deferToChild,
      child: Container(
        margin: EdgeInsets.only(bottom: 30.w),
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(bgMap[coupon.couponType]),
            fit: BoxFit.fill,
          ),
          boxShadow: [DEFAULT_SHADOW],
        ),
        child: Row(
          children: <Widget>[
            Container(
              width: 200.w,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  if (coupon.couponType == Coupon.TYPE_CASH) _buildCashTitle(),
                  if (coupon.couponType == Coupon.TYPE_DISCOUNT) _buildDiscountTitle(),
                  if (coupon.usingThreshold != 0)
                    Text(
                      '满${AmountUtil.price2String(coupon.usingThreshold)}元可用',
                      style: TextStyleConstant().white_24,
                      maxLines: 1,
                      overflow: TextOverflow.clip,
                    ),
                ],
              ),
            ),
            Expanded(
              child: Container(
                padding: EdgeInsets.only(left: 20.w),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: <Widget>[
                          Text(
                            coupon.couponName,
                            style: TextStyleConstant().normal_1_32,
                          ),
                          Row(
                            children: <Widget>[
                              Container(
                                padding: EdgeInsets.symmetric(horizontal: 3.5, vertical: 2),
                                decoration: BoxDecoration(
                                  border: Border.all(color: ColorConstant.fe8076, width: 1.px),
                                  borderRadius: BorderRadius.circular(30.w),
                                ),
                                alignment: Alignment.center,
                                child: Text(
                                  typeMap[coupon.couponType],
                                  style: TextStyleConstant().origin_22.copyWith(height: 1.15),
                                ),
                              ),
                            ],
                          ),
                          Text(
                            '有效日期：\r\n$timeRange',
                            style: TextStyleConstant().normal_3_22,
                          ),
                        ],
                      ),
                    ),
                    if (type == 0)
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 10.w),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            RadiusButton(
                              title: '发券',
                              width: 150.w,
                              height: 50.w,
                              color: ColorConstant.fe8076,
                              onTap: onSendCouponTap,
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 5),
                              child: RichText(
                                text: TextSpan(
                                  text: '剩余：',
                                  style: TextStyleConstant().normal_3_22,
                                  children: [
                                    TextSpan(text: '${coupon.couponTotalNum == null ? '无限' : coupon.couponSurplusNum}', style: TextStyleConstant().origin_22),
                                    TextSpan(text: '张'),
                                  ],
                                ),
                              ),
                            )
                          ],
                        ),
                      )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCashTitle() {
    return RichText(
      maxLines: 1,
      overflow: TextOverflow.clip,
      text: TextSpan(
        text: '¥',
        style: TextStyleConstant().white_32,
        children: [
          TextSpan(
            text: AmountUtil.price2String(coupon.couponDiscountAmount),
            style: TextStyle(
              fontSize: 60.sp,
              fontFamily: 'DINAlternate-Bold',
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }

  _buildDiscountTitle() {
    return RichText(
      text: TextSpan(
        text: '${coupon.couponDiscountAmount / 10}',
        style: TextStyle(
          fontSize: 60.sp,
          fontFamily: 'DINAlternate-Bold',
          fontWeight: FontWeight.bold,
        ),
        children: [
          TextSpan(
            text: '折',
            style: TextStyleConstant().white_30,
          )
        ],
      ),
    );
  }
}
